<?php if (!defined('BASEPATH'))

    exit('No direct script access allowed');

require_once APPPATH . "controllers/super" . EXT;

session_start();

Class Purchase extends super{

    

	/**

	 * Index Page for this controller.

	 *

	 * Maps to the following URL

	 * 		http://example.com/index.php/welcome

	 *	- or -  

	 * 		http://example.com/index.php/welcome/index

	 *	- or -

	 * Since this controller is set as the default controller in 

	 * config/routes.php, it's displayed at http://example.com/

	 *

	 * So any other public methods not prefixed with an underscore will

	 * map to /index.php/welcome/<method_name>

	 * @see http://codeigniter.com/user_guide/general/urls.html

	 */

     

    function __construct(){

        parent::__construct();

        //$this->load->model('user','',TRUE);

        $this->load->model(array('purchase_model','product_model'));

    }

    

    public function template(){

        $data = array();

        $this->load->helper(array('form'));
        
        $ata['title_page'] = $data['title_page'] = 'Purchase';
        
        $data['header']= $this->header($data['title_page']);

        $data['footer']= $this->footer();

        $data['navigate']= $this->navigate();
        
        $data['sidebarleft'] = $this->load->view('purchase/sidebarleft',$ata,true);
        
        $data['jspage'] = $this->load->view('purchase/js',null,true);

        return $data;

    }
    

    public function view(){

        $data = array();

        $data = $this->template();
        $data['title_page'] = $data['title_page'].' List';
        
        $data['js'] = $this->load->view('purchase/js',null,true); 
        
        $data['list'] = $this->purchase_model->getlist();

        $this->load->view('purchase/view',$data);

    }
    

    public function add(){

        $data = array(); 
        
        $data = $this->template();
        $data['title_page'] = $data['title_page'].' Add';
        //$data['liststaff'] = $this->leave_model->getliststaff(); 
        $submit = $this->input->post('submit');
        if(!empty($submit)){
            
            //PARAMS
            //-STATIS
            $code = $this->input->post('code');
            $date = $this->input->post('date');
            $duedate = $this->input->post('deudate');
            $supplier = $this->input->post('supplier');
            $description = $this->input->post('description');
            $payment = $this->input->post('payment');
            $address = $this->input->post('address');
            //-DYNAMIC
            $ordername = $this->input->post('ordername');
            $orderquantity = $this->input->post('orderquantity');
            $ordercost = $this->input->post('ordercost');
            $ordersell = $this->input->post('ordersell');
            $orderavailable = $this->input->post('orderavailable');
            $ordertotal = $this->input->post('ordertotal');
            

            //print_r($ordername);

            //PROCESS PARAM
            $date = str_replace('/','-',$date) ;
            $date = strtotime($date);
            
            $duedate = str_replace('/','-',$duedate) ;
            $duedate = strtotime($duedate);

            //SAVE
            $param = array();
            foreach ($ordername as $k => $value) {
                # code...
                $pro = explode('--',$value);

                $param['codepurchase']  = $code;
                $param['idproduct']     = $pro[0];
                $param['supplier']      = $supplier;
                $param['productpath']   = $value;
                $param['total']         = $ordertotal[$k];
                $param['quantity']      = $orderquantity[$k];
                $param['cost']          = $ordercost[$k];
                $param['sell']          = $ordersell[$k];
                $param['available']     = $orderavailable[$k];
                $param['date']          = $date;
                $param['duedate']       = $duedate;
                $param['payment']       = $payment;
                $param['address']       = $address;
                $param['note']          = $description;
                $param['class']         = 1;
                $param['datecreated']   = time();
                $param['dateupdated']   = time();
                $param['active']        = 1;

                //print_r($param);

                $this->purchase_model->additem($param);

                $this->product_model->update_available($param['idproduct'],$param['quantity']);


            }
        }
        $data['list'] = $this->purchase_model->getlist();
        
        $this->load->view('purchase/add',$data);  
    
    }

    

    public function update(){

        $data = array();
        
        $data = $this->template();
        $data['title_page'] = $data['title_page'].' Update';
        
        $last = $this->uri->total_segments();
        $id = $this->uri->segment($last);
        
        $data['purchase'] = $this->purchase_model->getitem(array('codepurchase'=>$id));

        //print_r($data['purchase']);
        
        $submit = $this->input->post('submit');
        if(!empty($submit)){
            
            //PARAMS
            //-STATIS
            $code = $this->input->post('code');
            $date = $this->input->post('date');
            $duedate = $this->input->post('deudate');
            $supplier = $this->input->post('supplier');
            $description = $this->input->post('description');
            $payment = $this->input->post('payment');
            $address = $this->input->post('address');
            //-DYNAMIC
            $ordername = $this->input->post('ordername');
            $orderquantity = $this->input->post('orderquantity');
            $ordercost = $this->input->post('ordercost');
            $ordersell = $this->input->post('ordersell');
            $orderavailable = $this->input->post('orderavailable');
            $ordertotal = $this->input->post('ordertotal');
            $orderidpurchase = $this->input->post('idpurchase');

            //OLD DB 
            $data_purchase = $this->purchase_model->getitem(array('codepurchase'=>$code));

            //print_r($data_purchase);

            //PROCESS PARAM
            $date = str_replace('/','-',$date) ;
            $date = strtotime($date);
            
            $duedate = str_replace('/','-',$duedate) ;
            $duedate = strtotime($duedate);

            //SAVE
            $param = array();
            foreach ($ordername as $k => $value) {
                # code...
                $pro = explode('--',$value);

                //$param['codepurchase']  = $code;
                $param['idproduct']     = $pro[0];
                $param['supplier']      = $supplier;
                //$param['productpath']   = $value;
                $param['total']         = $ordertotal[$k];
                $param['quantity']      = $orderquantity[$k];
                //$param['cost']          = $ordercost[$k];
                //$param['sell']          = $ordersell[$k];
                //$param['available']     = $orderavailable[$k];
                //$param['date']          = $date;
                $param['duedate']       = $duedate;
                $param['payment']       = $payment;
                $param['address']       = $address;
                $param['note']          = $description;
                //$param['class']         = 1;
                //$param['datecreated']   = time();
                $param['dateupdated']   = time();
                //$param['active']        = 1;

                //print_r($param);
                $idpurchase = $orderidpurchase[$k];
                $condition = array('idpurchase' => $idpurchase);

                $this->purchase_model->updateitem($param,$condition);

                $this->product_model->update_available($param['idproduct'],$param['quantity'],$data_purchase[$k]['quantity']);

                redirect("purchase/update/$code", 'refresh');
            }
            
        }
        
        $data['list'] = $this->purchase_model->getlist();
        
        $this->load->view('purchase/update',$data);  

    }

    

    public function delete(){
        $last = $this->uri->total_segments();
        $id = $this->uri->segment($last);

        $data_purchase  = $this->purchase_model->getitem(array('codepurchase'=>$id));
        print_r($data_purchase);
        foreach($data_purchase as $k => $v) {
            # code...
            $this->product_model->update_available($v['idproduct'],0,$v['quantity']);
        }
        

        $data['purchase'] = $this->purchase_model->deleteitem(array('codepurchase'=>$id));
        redirect("purchase/view", 'refresh');
    }

    public function action(){
        $checklist = $this->input->post('checklist');
        $action = $this->input->post('action');
         
        if($action == 'option1'){
            foreach($checklist as $id){
                $this->purchase_model->deleteitem(array('idpurchase'=>$id));
            }
        }
        
        redirect("purchase/view", 'refresh');
    }



}



/* End of file welcome.php */

/* Location: ./application/controllers/welcome.php */